.TH std::bitset::reference 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::bitset::reference \- std::bitset::reference

.SH Synopsis
   class reference;

   The std::bitset class includes std::bitset::reference as a publicly-accessible
   nested class. This class is used as a proxy object to allow users to interact with
   individual bits of a bitset, since standard C++ types (like references and pointers)
   are not built with enough precision to specify individual bits.

   The primary use of std::bitset::reference is to provide an lvalue that can be
   returned from operator[].

   Any reads or writes to a bitset that happen via a std::bitset::reference potentially
   read or write to the entire underlying bitset.

.SH Member functions

   constructor   constructs the reference. Accessible only to std::bitset itself
                 (private member function)
   destructor    destroys the reference
                 \fI(public member function)\fP
   operator=     assigns a bool to the referenced bit
                 \fI(public member function)\fP
   operator bool returns the referenced bit
                 \fI(public member function)\fP
   operator~     returns inverted referenced bit
                 \fI(public member function)\fP
   flip          flips the referenced bit
                 \fI(public member function)\fP

std::bitset<N>::reference::~reference

   ~reference();  (constexpr since C++23)

   Destroys the reference.

std::bitset<N>::reference::operator=

   reference& operator=( bool x );                    \fI(until C++11)\fP
                                                      \fI(since C++11)\fP
   reference& operator=( bool x ) noexcept;           (constexpr since
                                                      C++23)
   reference& operator=( const reference& x   \fB(1)\fP                      \fI(until C++11)\fP
   );
   reference& operator=( const reference& x )     \fB(2)\fP                  \fI(since C++11)\fP
   noexcept;                                                           (constexpr since
                                                                       C++23)

   Assigns a value to the referenced bit.

.SH Parameters

   x - value to assign

.SH Return value

   *this

std::bitset<N>::reference::operator bool

   operator bool() const;           \fI(until C++11)\fP
   operator bool() const noexcept;  \fI(since C++11)\fP
                                    (constexpr since C++23)

   Returns the value of the referenced bit.

.SH Parameters

   \fI(none)\fP

.SH Return value

   The referenced bit.

std::bitset<N>::reference::operator~

   bool operator~() const;           \fI(until C++11)\fP
   bool operator~() const noexcept;  \fI(since C++11)\fP
                                     (constexpr since C++23)

   Returns the inverse of the referenced bit.

.SH Parameters

   \fI(none)\fP

.SH Return value

   The inverse of the referenced bit.

std::bitset<N>::reference::flip

   reference& flip();           \fI(until C++11)\fP
   reference& flip() noexcept;  \fI(since C++11)\fP
                                (constexpr since C++23)

   Inverts the referenced bit.

.SH Parameters

   \fI(none)\fP

.SH Return value

   *this

.SH Example


// Run this code

 #include <bitset>
 #include <iostream>

 int main()
 {
     std::bitset<4> bs{0b1110};

     std::bitset<4>::reference ref = bs[2];

     auto info = [&](int id)
     {
         std::cout << id << ") bs: " << bs << "; ref bit: " << ref << '\\n';
     };

     info(1);
     ref = false;
     info(2);
     ref = true;
     info(3);
     ref.flip();
     info(4);
     ref = bs[1]; // operator=( const reference& x )
     info(5);

     std::cout << "6) ~ref bit: " << ~ref << '\\n';
 }

.SH Output:

 1) bs: 1110; ref bit: 1
 2) bs: 1010; ref bit: 0
 3) bs: 1110; ref bit: 1
 4) bs: 1010; ref bit: 0
 5) bs: 1110; ref bit: 1
 6) ~ref bit: 0

.SH See also

   operator[] accesses specific bit
              \fI(public member function)\fP
